package javolution.text;

import java.io.IOException;
import java.text.ParsePosition;
import javolution.Javolution;
import javolution.context.ObjectFactory;
import javolution.lang.ClassInitializer;
import javolution.lang.Reflection;
import javolution.util.FastMap;

/* loaded from: classes2.dex */
public abstract class TextFormat<T> {
    private static final FastMap FORMATS = new FastMap().setShared(true);

    /* loaded from: classes2.dex */
    public static class Cursor extends ParsePosition {
        private static final ObjectFactory FACTORY = new ObjectFactory() { // from class: javolution.text.TextFormat.Cursor.1
            @Override // javolution.context.ObjectFactory
            public Object create() {
                return new Cursor();
            }
        };
        private int _end;
        private int _index;
        private int _start;

        private Cursor() {
            super(0);
        }

        private final boolean match(String str, CharSequence charSequence) {
            int length = str.length();
            int i = this._end;
            int i2 = this._index + 1;
            int i3 = 1;
            while (i3 < length) {
                if (i2 >= i) {
                    return false;
                }
                int i4 = i2 + 1;
                int i5 = i3 + 1;
                if (charSequence.charAt(i2) != str.charAt(i3)) {
                    return false;
                }
                i2 = i4;
                i3 = i5;
            }
            return true;
        }

        public static Cursor newInstance(int i, int i2) {
            Cursor cursor = (Cursor) FACTORY.object();
            cursor._index = i;
            cursor._start = i;
            cursor._end = i2;
            cursor.setErrorIndex(-1);
            return cursor;
        }

        public static void recycle(Cursor cursor) {
            FACTORY.recycle(cursor);
        }

        public final boolean at(char c, CharSequence charSequence) {
            int i = this._index;
            return i < this._end && charSequence.charAt(i) == c;
        }

        public final boolean at(String str, CharSequence charSequence) {
            int i = this._index;
            if (i >= this._end || charSequence.charAt(i) != str.charAt(0)) {
                return false;
            }
            return match(str, charSequence);
        }

        public final boolean at(CharSet charSet, CharSequence charSequence) {
            int i = this._index;
            return i < this._end && charSet.contains(charSequence.charAt(i));
        }

        @Override // java.text.ParsePosition
        public boolean equals(Object obj) {
            return obj != null && (obj instanceof Cursor) && this._index == ((Cursor) obj)._index;
        }

        public final int getEndIndex() {
            return this._end;
        }

        @Override // java.text.ParsePosition
        public final int getErrorIndex() {
            int errorIndex = getErrorIndex();
            return errorIndex >= 0 ? errorIndex : this._index;
        }

        @Override // java.text.ParsePosition
        public final int getIndex() {
            return this._index;
        }

        public final int getStartIndex() {
            return this._start;
        }

        public final boolean hasNext() {
            return this._index < this._end;
        }

        @Override // java.text.ParsePosition
        public int hashCode() {
            return this._index;
        }

        public final Cursor increment() {
            this._index++;
            return this;
        }

        public final Cursor increment(int i) {
            this._index += i;
            return this;
        }

        public final char next(CharSequence charSequence) {
            int i = this._index;
            if (i < this._end) {
                this._index = i + 1;
                return charSequence.charAt(i);
            }
            return (char) 0;
        }

        public final void setEndIndex(int i) {
            this._end = i;
        }

        @Override // java.text.ParsePosition
        public final void setErrorIndex(int i) {
            super.setErrorIndex(i);
        }

        @Override // java.text.ParsePosition
        public final void setIndex(int i) {
            if (i < this._start || i > this._end) {
                throw new IllegalArgumentException();
            }
            this._index = i;
        }

        public final void setStartIndex(int i) {
            this._start = i;
        }

        public final boolean skip(char c, CharSequence charSequence) {
            while (true) {
                int i = this._index;
                if (i >= this._end || charSequence.charAt(i) != c) {
                    break;
                }
                this._index++;
            }
            return this._index < this._end;
        }

        public final boolean skip(CharSet charSet, CharSequence charSequence) {
            while (true) {
                int i = this._index;
                if (i >= this._end || !charSet.contains(charSequence.charAt(i))) {
                    break;
                }
                this._index++;
            }
            return this._index < this._end;
        }

        @Override // java.text.ParsePosition
        public String toString() {
            return String.valueOf(this._index);
        }
    }

    static {
        FORMATS.put(new Boolean(true).getClass(), new TextFormat() { // from class: javolution.text.TextFormat.1
            @Override // javolution.text.TextFormat
            public Appendable format(Object obj, Appendable appendable) throws IOException {
                return TypeFormat.format(((Boolean) obj).booleanValue(), appendable);
            }

            @Override // javolution.text.TextFormat
            public Object parse(CharSequence charSequence, Cursor cursor) {
                return new Boolean(TypeFormat.parseBoolean(charSequence, cursor));
            }
        });
        FORMATS.put(new Character(' ').getClass(), new TextFormat() { // from class: javolution.text.TextFormat.2
            @Override // javolution.text.TextFormat
            public Appendable format(Object obj, Appendable appendable) throws IOException {
                return appendable.append(((Character) obj).charValue());
            }

            @Override // javolution.text.TextFormat
            public Object parse(CharSequence charSequence, Cursor cursor) {
                return new Character(cursor.next(charSequence));
            }
        });
        FORMATS.put(new Byte((byte) 0).getClass(), new TextFormat() { // from class: javolution.text.TextFormat.3
            @Override // javolution.text.TextFormat
            public Appendable format(Object obj, Appendable appendable) throws IOException {
                return TypeFormat.format((int) ((Byte) obj).byteValue(), appendable);
            }

            @Override // javolution.text.TextFormat
            public Object parse(CharSequence charSequence, Cursor cursor) {
                return new Byte(TypeFormat.parseByte(charSequence, 10, cursor));
            }
        });
        FORMATS.put(new Short((short) 0).getClass(), new TextFormat() { // from class: javolution.text.TextFormat.4
            @Override // javolution.text.TextFormat
            public Appendable format(Object obj, Appendable appendable) throws IOException {
                return TypeFormat.format((int) ((Short) obj).shortValue(), appendable);
            }

            @Override // javolution.text.TextFormat
            public Object parse(CharSequence charSequence, Cursor cursor) {
                return new Short(TypeFormat.parseShort(charSequence, 10, cursor));
            }
        });
        FORMATS.put(new Integer(0).getClass(), new TextFormat() { // from class: javolution.text.TextFormat.5
            @Override // javolution.text.TextFormat
            public Appendable format(Object obj, Appendable appendable) throws IOException {
                return TypeFormat.format(((Integer) obj).intValue(), appendable);
            }

            @Override // javolution.text.TextFormat
            public Object parse(CharSequence charSequence, Cursor cursor) {
                return new Integer(TypeFormat.parseInt(charSequence, 10, cursor));
            }
        });
        FORMATS.put(new Long(0L).getClass(), new TextFormat() { // from class: javolution.text.TextFormat.6
            @Override // javolution.text.TextFormat
            public Appendable format(Object obj, Appendable appendable) throws IOException {
                return TypeFormat.format(((Long) obj).longValue(), appendable);
            }

            @Override // javolution.text.TextFormat
            public Object parse(CharSequence charSequence, Cursor cursor) {
                return new Long(TypeFormat.parseLong(charSequence, 10, cursor));
            }
        });
        FORMATS.put("".getClass().getClass(), new TextFormat() { // from class: javolution.text.TextFormat.7
            @Override // javolution.text.TextFormat
            public Appendable format(Object obj, Appendable appendable) throws IOException {
                return appendable.append(Javolution.j2meToCharSeq(((Class) obj).getName()));
            }

            @Override // javolution.text.TextFormat
            public Object parse(CharSequence charSequence, Cursor cursor) {
                Text valueOf = Text.valueOf(charSequence.subSequence(cursor.getIndex(), cursor.getEndIndex()));
                int indexOfAny = valueOf.indexOfAny(CharSet.WHITESPACES);
                if (indexOfAny < 0) {
                    indexOfAny = valueOf.length();
                }
                Text subtext = valueOf.subtext(0, indexOfAny);
                try {
                    Class cls = Reflection.getClass(subtext);
                    cursor.increment(indexOfAny);
                    return cls;
                } catch (ClassNotFoundException unused) {
                    throw new IllegalArgumentException("Class " + ((Object) subtext) + " Not Found");
                }
            }
        });
        FORMATS.put(new Float(0.0f).getClass(), new TextFormat() { // from class: javolution.text.TextFormat.8
            @Override // javolution.text.TextFormat
            public Appendable format(Object obj, Appendable appendable) throws IOException {
                return TypeFormat.format(((Float) obj).floatValue(), appendable);
            }

            @Override // javolution.text.TextFormat
            public Object parse(CharSequence charSequence, Cursor cursor) {
                return new Float(TypeFormat.parseFloat(charSequence, cursor));
            }
        });
        FORMATS.put(new Double(0.0d).getClass(), new TextFormat() { // from class: javolution.text.TextFormat.9
            @Override // javolution.text.TextFormat
            public Appendable format(Object obj, Appendable appendable) throws IOException {
                return TypeFormat.format(((Double) obj).doubleValue(), appendable);
            }

            @Override // javolution.text.TextFormat
            public Object parse(CharSequence charSequence, Cursor cursor) {
                return new Double(TypeFormat.parseDouble(charSequence, cursor));
            }
        });
    }

    protected TextFormat() {
    }

    public static <T> TextFormat<T> getInstance(Class<T> cls) {
        TextFormat<T> textFormat = (TextFormat) FORMATS.get(cls);
        return textFormat != null ? textFormat : searchFormat(cls);
    }

    private static TextFormat searchFormat(Class cls) {
        if (cls == null) {
            return null;
        }
        ClassInitializer.initialize(cls);
        TextFormat textFormat = (TextFormat) FORMATS.get(cls);
        return textFormat != null ? textFormat : searchFormat(superclassOf(cls));
    }

    public static <T> void setInstance(Class<T> cls, TextFormat<T> textFormat) {
        ClassInitializer.initialize(cls);
        FORMATS.put(cls, textFormat);
    }

    private static Class superclassOf(Class cls) {
        return cls.getSuperclass();
    }

    public abstract Appendable format(T t, Appendable appendable) throws IOException;

    public final Appendable format(T t, TextBuilder textBuilder) {
        try {
            return format((TextFormat<T>) t, (Appendable) textBuilder);
        } catch (IOException unused) {
            throw new Error();
        }
    }

    public final Text format(T t) {
        TextBuilder newInstance = TextBuilder.newInstance();
        format((TextFormat<T>) t, newInstance);
        Text text = newInstance.toText();
        TextBuilder.recycle(newInstance);
        return text;
    }

    public final T parse(CharSequence charSequence) {
        Cursor newInstance = Cursor.newInstance(0, charSequence.length());
        T parse = parse(charSequence, newInstance);
        if (newInstance.hasNext()) {
            throw new IllegalArgumentException("Incomplete Parsing");
        }
        Cursor.recycle(newInstance);
        return parse;
    }

    public abstract T parse(CharSequence charSequence, Cursor cursor);
}
